package cn.xspace.wnx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.base.BaseHolder;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.ResourceUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.xspace.library.bean.Type;
import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.GroupEntity;

/**
 * Wubj 创建于 2016/12/27 0027.
 */
public class MyGroupsAdapter extends BaseAdapter {

    private static final String TAG = MyGroupsAdapter.class.getSimpleName();

    private static final int TYPE_TITLE = 0;
    private static final int TYPE_CONTENT = 1;

    private Context mContext;
    private HashMap<Integer, List<GroupEntity>> mDataMap;
    private List<GroupEntity> proGroups = new ArrayList<>();
    private List<GroupEntity> comGroups = new ArrayList<>();
    private String[] groupsType = ResourceUtils.getStringArray(R.array.group_type);

    public MyGroupsAdapter(Context context, HashMap<Integer, List<GroupEntity>> mDataMap) {
        mContext = context;
        this.mDataMap = mDataMap;
        proGroups = mDataMap.get(Type.GroupType.PROFESSIONAL);
        comGroups = mDataMap.get(Type.GroupType.NORMAL);
        LogUtil.d(TAG, "wubaojie>>>MyGroupsAdapter: pro: " + proGroups.size() + " com: " + comGroups.size());
    }

    @Override
    public int getCount() {
        return comGroups.size() + proGroups.size() + mDataMap.size();
    }

    @Override
    public int getViewTypeCount() {
        return mDataMap.size();
    }

    @Override
    public Object getItem(int position) {
        if (position == 0) {
            return String.format(ResourceUtils.getString(R.string.pro_group_count), proGroups.size()); //专业群组条目
//            return groupsType[1]; //专业群组条目
        } else if (position == proGroups.size() + 1) {
            return String.format(ResourceUtils.getString(R.string.com_group_count), comGroups.size()); //普通群组条目
//            return groupsType[2]; //普通群组条目
        } else {
            if (position <= proGroups.size()) {
                return proGroups.get(position - 1);
            } else {
                return comGroups.get(position - 1 - proGroups.size() - 1);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 || position == proGroups.size() + 1) {
            return TYPE_TITLE;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseHolder<Object> holder;
        if (convertView == null) {
            holder = getHolder(position);
            //在holder中创建一个方法,返回器中的holderView,用作这里的convertView
            convertView = holder.getHolderView();
        } else {
            holder = (BaseHolder<Object>) convertView.getTag();
        }
        holder.bindData(getItem(position), position);
//        TitleViewHolder tHolder = null;
//        ContentViewHolder cHolder = null;
//        int type = getItemViewType(position);
//        LogUtil.d(TAG, "wubaojie>>>getView: type:" + type);
//        if (convertView == null) {
//            switch (type) {
//                case TYPE_TITLE:
//                    tHolder = new TitleViewHolder();
//                    convertView = tHolder.getHolderView();
//                    break;
//                case TYPE_CONTENT:
//                    cHolder = new ContentViewHolder();
//                    convertView = cHolder.getHolderView();
//                    break;
//            }
//        } else {
//            switch (type) {
//                case TYPE_TITLE:
//                    tHolder = (TitleViewHolder) convertView.getTag();
//                    break;
//                case TYPE_CONTENT:
//                    cHolder = (ContentViewHolder) convertView.getTag();
//                    break;
//            }
//        }
//        switch (type) {
//            case TYPE_TITLE:
//                tHolder.bindData(getItem(position));
//                break;
//            case TYPE_CONTENT:
//                cHolder.bindData(getItem(position));
//                break;
//        }
        return convertView;
    }

    private BaseHolder<Object> getHolder(int position) {
        BaseHolder<Object> holder = null;
        int type = getItemViewType(position);
        switch (type) {
            case TYPE_TITLE:
                holder = new TitleViewHolder();
                break;
            case TYPE_CONTENT:
                holder = new ContentViewHolder();
                break;
        }
        return holder;
    }

    class ContentViewHolder extends BaseHolder<Object> {

        ImageView headImg;
        TextView nameTxt;

        @Override
        protected View initHolderView() {
            View view = View.inflate(mContext, R.layout.listitem_group, null);
            headImg = (ImageView) view.findViewById(R.id.img_head);
            nameTxt = (TextView) view.findViewById(R.id.txt_name);
            return view;
        }

        @Override
        public void bindData(Object info, int position) {
            GroupEntity entity = (GroupEntity) info;
            switch (entity.group_type) {
                case Type.GroupType.ORGANIZATION:
                    headImg.setImageResource(R.drawable.icon_default_org);
                    break;
                case Type.GroupType.PROFESSIONAL:
                    headImg.setImageResource(R.drawable.icon_default_profess);
//                    Picasso.with(mContext).load(R.drawable.icon_default_profess).into(headImg);
                    break;
                case Type.GroupType.NORMAL:
                    if (TextUtils.isEmpty(entity.group_logo)) {
                        headImg.setImageResource(R.drawable.icon_header_default);
                    } else {
                        Picasso.with(mContext)
                                .load(entity.group_logo)
                                .placeholder(R.drawable.icon_header_default)
                                .error(R.drawable.icon_header_default)
                                .into(headImg);
                    }
                    break;
            }
            nameTxt.setText(entity.group_name);
        }
    }

    class TitleViewHolder extends BaseHolder<Object> {

        TextView title;

        @Override
        protected View initHolderView() {
            View view = View.inflate(mContext, R.layout.listitem_groups_title, null);
            title = (TextView) view.findViewById(R.id.tv_item_appinfo_title);
            return view;
        }

        @Override
        public void bindData(Object info, int position) {
            String groupTitle = (String) info;
            String[] split = groupTitle.split("@");
            if (Integer.parseInt(split[1]) == 0) {
                title.setVisibility(View.GONE);
            } else {
                title.setText(split[0]);
            }
        }
    }

}
